﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Security.Cryptography;
using DBBase;
using System.Text.RegularExpressions;
namespace Business
{
    public partial class EditUser : Form
    {
        public MainForm m_mainform;

        public static string suid;
        public static string mima;
        public static string sip;
        public bool bmima;
        public bool bzhanghao;

        public EditUser(string ip)
        {
            InitializeComponent();
            sip = ip;
            
        }
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
       

        private void but_mima_Click(object sender, EventArgs e)
        {

            mimachongzhi m_mima = new mimachongzhi(suid, text_yhxm.Text, text_jybh.Text, mima, text_zhanghao.Text);
            m_mima.m_mainform = m_mainform;
            m_mima.Show();
        }

        

        private void text_zhanghao_Validating(object sender, CancelEventArgs e)
        {
            

        }
        public void baocunshrj(string saqcl, string syonghuming, string srjnr)// 安全策略 用户名 日记内容
        {
            Query objquery = new Query();
            string ssql = "";

            ssql = string.Format(@"insert into shenjiriji (shrj_aqcl,shrj_yhdh,shrj_rjnr,shrj_sjsj) values 
                                    ('{0}',
                                    '{1}',
                                    '{2}',
                                    '{3}')",
                                    saqcl,
                                    syonghuming,
                                    srjnr,
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("添加失败");
            }
        }
        
        public void baocunhxrj(string saqcl, string syonghuming, string srjnr)// 核心功能日记 用户名 日记内容
        {
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"insert into hexinriji (shrj_aqcl,shrj_yhdh,shrj_yhip,shrj_rjnr,shrj_sjsj) values 
                                    ('{0}',
                                    '{1}',
                                    '{2}',
                                    '{3}',
                                    '{4}')",
                                    saqcl,
                                    syonghuming,
                                    sip,
                                    Encryption(srjnr),
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("添加失败");
            }
        }

        private void text_sfzh_Validating(object sender, CancelEventArgs e)
        {
  
            if ((!Regex.IsMatch(text_sfzh.Text, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase)))
            {
                MessageBox.Show("身份证号格式不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
          //  bmima = true;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
          //  bzhanghao = true;
        }
        public void genggaixgbs(string sname)
        {
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"update  USERE set XGJLBS='是' where NAME='{0}'", sname);
            objquery.UpdateTable(ssql);
        }
        private void text_zhanghao_TextChanged(object sender, EventArgs e)
        {
            string sql = string.Format(@"Select * From USERE where YHLB='{0}' ", text_zhanghao.Text);

            Query mquery = new Query();
            DataTable dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                string sxgsfbs = dt.Rows[0]["XGJLBS"].ToString().Trim();
                string szhm=Decrypt(dt.Rows[0]["ZHMJM"].ToString().Trim());
                string name = dt.Rows[0]["NAME"].ToString().Trim();
                if (text_zhanghao.Text == szhm)//
                {
                    suid = dt.Rows[0]["UID"].ToString().Trim();
                    mima = dt.Rows[0]["PASSWORD"].ToString().Trim();
                    text_sfzh.Text = dt.Rows[0]["SFZHM"].ToString().Trim();
                    text_yhxm.Text = dt.Rows[0]["NAME"].ToString().Trim();
                    text_jybh.Text = dt.Rows[0]["GZRYBH"].ToString().Trim();
                    text_dlip.Text = string.Format(@"{0};{1};{2}", dt.Rows[0]["GDIP1"].ToString().Trim(), dt.Rows[0]["GDIP2"].ToString().Trim(), dt.Rows[0]["GDIP3"].ToString().Trim());


                    text_jybh.Text = dt.Rows[0]["GZRYBH"].ToString().Trim();
                    dateTimePicker2.Value = Convert.ToDateTime(dt.Rows[0]["MMYXQ"].ToString().Trim());
                    dateTimePicker1.Value = Convert.ToDateTime(dt.Rows[0]["YHYXQ"].ToString().Trim());

                    dateTimePicker3.Value = Convert.ToDateTime(dt.Rows[0]["JSSJ"].ToString().Trim());
                    dateTimePicker4.Value = Convert.ToDateTime(dt.Rows[0]["QSSJ"].ToString().Trim());

                    if (dt.Rows[0]["SFMJ"].ToString().Trim().Contains("是"))
                    {
                        check_sfmj.Checked = true;
                    }
                    else
                    {
                        check_sfmj.Checked = false;
                    }

                    if (dt.Rows[0]["ZT"].ToString().Trim().Contains("在用"))
                    {
                        radio_ztzy.Checked = true;

                        radio_ztsd.Checked = false;
                        radio_ztty.Checked = false;
                    }
                    else if (dt.Rows[0]["ZT"].ToString().Trim().Contains("锁定"))
                    {
                        radio_ztzy.Checked = false;

                        radio_ztsd.Checked = true;
                        radio_ztty.Checked = false;
                    }
                    else if (dt.Rows[0]["ZT"].ToString().Trim().Contains("停用"))
                    {
                        radio_ztzy.Checked = false;

                        radio_ztsd.Checked = false;
                        radio_ztty.Checked = true;
                    }
                    text_bz.Text = dt.Rows[0]["BZ"].ToString().Trim();

                    text_zjdlsj.Text = dt.Rows[0]["ZJDLSJ"].ToString().Trim();
                    com_JueShe.Text = dt.Rows[0]["JSLX"].ToString().Trim();

                    string syhlx = dt.Rows[0]["YHJS"].ToString().Trim();
                    text_jslx.Text = dt.Rows[0]["YHJS"].ToString().Trim();
                    if (syhlx.Contains("系统管理"))
                    {
                        com_JueShe.Items.Clear();
                        com_JueShe.Items.Add("系统管理员");
                        com_JueShe.Items.Add("系统默认操作员");

                    }
                    else if (syhlx.Contains("安全管理"))
                    {
                        com_JueShe.Items.Clear();
                        com_JueShe.Items.Add("安全管理员");

                    }
                    else if (syhlx.Contains("审计管理"))
                    {
                        com_JueShe.Items.Clear();
                        com_JueShe.Items.Add("审计管理员");
                        com_JueShe.Items.Add("核心操作审计管理员");
                    }
                    else if (syhlx.Contains("业务"))
                    {
                        com_JueShe.Items.Clear();
                        com_JueShe.Items.Add("管理员");
                        com_JueShe.Items.Add("登录员");
                        com_JueShe.Items.Add("出表员");
                        com_JueShe.Items.Add("引车员");
                        com_JueShe.Items.Add("底盘检验员");
                        com_JueShe.Items.Add("动态检验员 ");
                        com_JueShe.Items.Add("外检员");
                    }
                }
                else
                {
                    if (sxgsfbs == "")
                    {
                        //记录安全日志
                      //  baocunaqrj("数据被篡改", szhm, "账户数据被修改校验不通过");
                        m_mainform.baocunaqrj("数据被篡改", "更改", "系统管理", "否", "否", "失败", string.Format("账户{0}数据被修改校验不通过", szhm));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

                        //更新标识
                        genggaixgbs(name);
                    }
                }


            }
            else
            {
                suid = "";
                mima = "";
                text_sfzh.Text = "";
                text_yhxm.Text = "";
                text_jybh.Text = "";
               

                text_jybh.Text = "";
               
                    radio_ztzy.Checked = false;

                    radio_ztsd.Checked = false;
                    radio_ztty.Checked = false;
                
                text_bz.Text = "";

                text_zjdlsj.Text = "";
                com_JueShe.Text = "";

                
                text_jslx.Text = "";
              
            }
            bmima = false;
            bzhanghao = false;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (bmima)
            {
                MessageBox.Show("密码到期后延期需重置密码");
            }
            if (bzhanghao)
            {
                MessageBox.Show("帐号到期后延期需重置密码");
            }
            string szhuangtai = "在用";
            if (radio_ztzy.Checked)
            {
                szhuangtai = "在用";
            }
            else if (radio_ztsd.Checked)
            {
                szhuangtai = "锁定";
            }
            else if (radio_ztty.Checked)
            {
                szhuangtai = "停用";
            }
            string ssfmj = "否";
            if (check_sfmj.Checked)
            {
                ssfmj = "是";
            }
            else
            {
                ssfmj = "否";
            }
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select count(*) From USERE where YHLB='{0}'", text_zhanghao.Text);
            if (!objquery.ExecuteS(ssql))
            {
                MessageBox.Show("未存在账户名{0}", text_zhanghao.Text);
            }
            else
            {
                ssql = string.Format(@"update  USERE 
                                        set JSLX='{0}',
                                        ZZJG='{1}',
                                        YHLB='{2}',
                                        SFZHM='{3}',
                                        GZRYBH='{4}',
                                        BZ='{5}',
                                        MMYXQ='{6}',
                                        YHYXQ='{7}',
                                        GDIP1='{8}',
                                        GDIP2='{9}',
                                        GDIP3='{10}',
                                        QSSJ='{11}',
                                        JSSJ='{12}',
                                        ZT='{13}',
                                        SFMJ='{14}' where UID={15}", com_JueShe.Text,
                                        com_zjjg.Text,
                                        text_zhanghao.Text,
                                        text_sfzh.Text,
                                        text_jybh.Text,
                                        text_bz.Text,
                                        dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                                        dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                                        text_dlip.Text.Split(new char[] { ';' })[0],
                                        text_dlip.Text.Split(new char[] { ';' })[1],
                                        text_dlip.Text.Split(new char[] { ';' })[2],
                                        dateTimePicker4.Value.ToString("HH:mm:ss"),
                                        dateTimePicker3.Value.ToString("HH:mm:ss"),
                                        szhuangtai, ssfmj, suid);
                if (!objquery.UpdateTable(ssql))
                {
                    MessageBox.Show("用户编辑失败");
                }
                else
                {
                    MessageBox.Show("用户编辑成功");
                }
                string sneirong = "";
                sneirong = string.Format(@"用户于{0}进行了角色编辑操作，更新记录内容{1}表", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "USERE");
              //  baocunhxrj("角色编辑", text_zhanghao.Text, sneirong);

                //判断是否核心功能
                if (m_mainform.shxgn.Contains("用户管理编辑"))
                {
                    m_mainform.baocunhxrj("用户管理编辑", string.Format("编辑用户{0}:update  USERE set JSLX='' where UID={1}", text_zhanghao.Text, com_JueShe.Text), "更新");// 核心功能日记  日记内容
                }
                //保存操作日记
                m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "用户管理编辑");
            }
        }

        private void but_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EditUser_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("用户管理编辑"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_save.Enabled = false;
                }
            }
        }
    }
}
